<template>
  <div v-if="flag" class="management">
    <div class="item" v-for="(item, index) in params" :key="index">
      <div class="title">{{ item.title }}</div>
      <div class="chart">
        <line-chart :optionData="item.optionData" :dark="true"></line-chart>
      </div>
    </div>
  </div>
</template>

<script>
import { getLineData, getLineData1 } from "@/api/management";
import pubsub from "pubsub-js";

export default {
  name: "management",
  data() {
    return {
      params: null,
      flag: false,
    };
  },
  methods: {
    async init() {
      const ids = JSON.parse(localStorage.getItem("checkIds"));
      this.params = JSON.parse(localStorage.getItem("optionData"));
      const taskId = localStorage.getItem("taskId");
      if (this.params && ids.length > 0) {
        for (let item of this.params) {
          let res;
          if (taskId === "20240613") {
            res = await getLineData1({
              fields: item.fields,
              ids: ids,
            });
          } else {
            res = await getLineData({
              fields: item.fields,
              ids: ids,
            });
          }
          this.$set(item, "optionData", res.data); // 确保响应式
        }
        this.flag = true;
      } else {
        this.$modal.msgError("未选择对比人员或对比参数！");
      }
    },
  },
  created() {
    pubsub.subscribe("echarts", () => {
      this.init();
    });
    localStorage.removeItem("checkIds");
    localStorage.removeItem("optionData");
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.management {
  padding: 2% 3%;
  & > div + div {
    margin-top: 24px;
  }
  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 16px;
      color: #b2bec2;
    }
    .chart {
      width: 95%;
      height: 200px;
      overflow: auto;

      &::-webkit-scrollbar {
        display: block !important;
        height: 4px;
      }

      &::-webkit-scrollbar-thumb {
        background-color: #666;
        border-radius: 2px;
      }
    }
  }
}
</style>
